package com.fss.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.fss.model.TrainResourceModel;
import com.fss.utils.ExcelImportUtil;
import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;


/**
 * 
 * @author Temoly
 *资源的增删改查（管理员）
 *具体的资源的章和节属于修饰性信息，所以正正识别一个资源应该是对应的一个科目
 *
 */
public class TrainResourceController extends Controller{
	public static String getCurrentTime()
	  {
	    String format = "yyyyMMddHHmmSS";
	    Date date = new Date();
	    date.setTime(System.currentTimeMillis());
	    if ((format == null) || ("".equals(format.trim()))) {
	     format = "yyyy-MM-dd HH:mm:ss";
	   }
	    SimpleDateFormat sdf = new SimpleDateFormat(format);
	    return sdf.format(date);
	   }
	public void index(){
	List<Record> subjectList=TrainResourceModel.DAO.getList("0");
	System.out.println(subjectList);
	setAttr("subjectList",subjectList);
	render("getSubjectList.jsp");
	}
	//此上传是批量上传文件的写法
	public void saveSubject(){
		String dirName = getCurrentTime();
		String contextPath = PathKit.getWebRootPath();
		String path = "/upload/subject/" + dirName + "/";
		//服务器路径+自定义局部具体文件存放路径
		String pathUrl = contextPath + path;
		Map <String, Object> map= new LinkedHashMap();
		try{
		List<UploadFile> files = getFiles("subject/" + dirName);
		String subjectName=getPara("subjectName");
		String subjectDescribe=getPara("subjectDescribe");	
		String remark=getPara("remark");
		map=TrainResourceModel.DAO.uploadSubject(subjectName,files, pathUrl, subjectDescribe,path,remark,pathUrl); 
		renderJson(map);
		}
		catch(Exception e){
			map.put("status", "0");//0代表上传失败
			map.put("msg", "上传失败");
		    e.printStackTrace();
			ExcelImportUtil.deleteDir(new File(pathUrl));
			renderJson(map);
		}
	}
	}

	